/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class VNExpressXPath extends XPath {

    public VNExpressXPath(String xmlFile, String root, String loai) {
        this.xmlFile = xmlFile;
        if ("div".equals(loai)) {
            this.rootroot = "//*[@id=\"content\"]/div[1]/div[2]";
            this.root = rootroot + "/div[" + root + "]";
            this.link = this.root + "/div[2]/h2/a[1]";
            this.title = this.root + "/div[2]/h2/a[1]";
            this.img = this.root + "/div/a/img";
            this.date = this.root + "/div[2]/p/span";
            this.summary = this.root + "/div[2]/h3";
        } else if ("li".equals(loai)) {
            /////*[@id="cate_news_cap3"]/div/ul
            this.rootroot = "//*[@id=\"cate_news_cap3\"]/div/ul";
            this.root = rootroot + "/li[" + root + "]";
            this.link = this.root + "/div/p[1]/a[1]";
            this.title = this.root + "/div[1]/p/a/b";
            this.img = this.root + "/div/a/img";
            this.date = this.root + "/div/p[2]";
            this.summary = this.root + "/div/p[3]";
        }

    }

    public VNExpressXPath(String xmlFile, String loai) {

        this.xmlFile = xmlFile;
        if ("div".equals(loai)) {
            this.rootroot = "//*[@id=\"content\"]/div[1]/div[2]/*";
        } else {
            this.rootroot = "//*[@id=\"cate_news_cap3\"]/div[1]/ul/*";
        }
    }

    public VNExpressXPath() {
    }

    public VNExpressTheGioiA getBaiBao(int x) {

        VNExpressTheGioiA tg = new VNExpressTheGioiA();
        return tg;
    }

    public int Count() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.rootroot;
            NodeList node = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            return node.getLength()-1;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (IOException e) {
        }
        return 0;
    }

    public String getLink() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.link;
            Element node = (Element) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getAttribute("href");
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (IOException e) {
        }
        return null;

    }

    public void setLink(String link) {
        this.link = link;
    }

    @Override
    public String getTitle() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.title;
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getTextContent();
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImg() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.img;
            Element node = (Element) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getAttribute("src");
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;

    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getDate() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.date;
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getTextContent();
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;

    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSummary() {
        try {
            FileInputStream file = new FileInputStream(new File(this.xmlFile));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = this.summary;
            Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);
            String tmp = node.getTextContent();
            return tmp;
        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (Exception e) {
        }
        return null;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getXmlFile() {
        return xmlFile;
    }

    public void setXmlFile(String xmlFile) {
        this.xmlFile = xmlFile;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }
}
